<?php
// created by: alfin akhret
// files dependency:
// - region_cluster.js
// - cluster.php (controller)
// - mcluster.php (model)
// - f-cluster.php (view)
// - smooth.php (template)
class Cluster extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model('mmaster');
		$this->load->model('mcluster');
		$this->load->model('mglobal');
		$this->load->helper('form');
		$this->load->library('form_validation');
		
	}

	function index()
	{
		$data['template'] = "shell/smooth";
		$data['main_view'] = 'f-cluster';
		$data['cluster_id'] ="";
		$data['cluster_name'] ="";
		$data['cluster_parent_id'] ="";
		$data['region_name'] ="";
		$data['region_id'] ="";
		$data['status'] ="";
		$data['logic'] = "";
		$data['cluster_array'] = "";
		$data['region_array'] = "";

		$territory_type_id = $this->get_user_territory_type_id($this->session->userdata('group'));
		//echo $territory_type_id;  die(); 
		//$territory_type_id = '0';
		$username = $this->session->userdata('username');
		
		//jika territory_type_id = 3/5/6
		if($territory_type_id == '3' || $territory_type_id == '5' || $territory_type_id == '6')
		{
			$cluster = $this->get_territory('cluster', $username, '');
			foreach ($cluster as $r) {
				$data['cluster_id'] = $r->territory_id;
				$data['cluster_name'] = $r->territory_name;
				$data['cluster_parent_id'] = $r->parent_id;
			}
			$region = $this->get_territory('region', $username, $data['cluster_parent_id']);
			foreach ($region as $r => $s) {
				$data['region_id'] = $s->territory_id . "<br>";
				$data['region_name'] = $s->territory_name;
			}

			$data['status'] = 'disabled';
			$data['logic'] = '1'; // //jika territory_type_id = 3/5/6

			$a = array('logic' => '1',
						'region_name' => $data['region_name'],
						'territory_name' => $data['cluster_name']);
			$this->session->set_userdata($a); //ganti tampilan menu "pilih territory" menjadi nama region dan cluster nya

			$this->load->view($data['template'], $data);
		}
		else if($territory_type_id == '2')
		{
			$region = $this->get_territory('region', $username, '');
			foreach ($region as $r) {
				$data['region_id'] = $r->territory_id;
				$data['region_name'] = $r->territory_name;
			}
			
			$data['cluster_array'] = $this->get_territory('cluster', $username, $data['region_id']);
			$data['logic'] = '2'; // //jika territory_type_id = 2
			$this->load->view($data['template'], $data);
		}
		else
		{
			$data['region_array'] = $this->mcluster->get_regions('2');
			$this->load->view($data['template'], $data);
		}

		
	}

	function get_user_territory_type_id()
	{
		$result = $this->mcluster->get_user_territory_type_id($this->session->userdata('group'));
		foreach ($result as $r) {
			 return $r->territory_type_id;
		}
	}

	function get_territory($type, $username, $territory_id)
	{
		$result = $this->mcluster->get_territory($type, $username, $territory_id);
		return $result;
	}
	
	// function get_region_id($cluster_id)
	// {
	// 	$result = $this->mcluster->get_region_id($cluster_id);
	// 	foreach ($result as $r) {
	// 		return $r->parent_id;
	// 	}

	// }

	// function get_region_name($region_id)
	// {
	// 	$result = $this->mcluster->get_region_name($region_id);
	// 	foreach ($result as $r) {
	// 		return $r->territory_name;
	// 	}
	// }

	function get_cluster($territory_type_id, $parent_id)
	{
		$result = $this->mcluster->get_clusters($territory_type_id, $parent_id);
		echo json_encode($result);
	}

	function insert_session_cluster()
	{
		$this->form_validation->set_rules('region', 'Region', 'required');
		$this->form_validation->set_rules('cluster', 'Cluster', 'required');

		$user_id = $this->session->userdata('username');
		$territory_id = $this->input->post('cluster_id');

		$result = $this->mcluster->insert_session_cluster($user_id, $territory_id);
		// if($result)
		// {

		// 	// $cluster_name = $this->mcluster->get_cluster($territory_id);
		// 	// foreach ($cluster_name as $r) {
		// 	// 	$data['cluster_name'] = $r->territory_name;
		// 	// }
			
			
		// }

		$sess_item = $this->mcluster->get_cluster_data($territory_id);
		$temp = $this->mcluster->get_cluster_data($sess_item[0]['parent_id']);
		unset($sess_item[0]['parent_id']);
		@$sess_item[0]['region_name']=$temp[0]['territory_name'];
		$this->session->set_userdata($sess_item[0]);
		//log
		$this->mglobal->saveLog('setting territory', 'changes to territory : '.$this->session->userdata("region_name").' , '.$this->session->userdata("territory_name"), $this->session->userdata("username"));
		//redirect('index.php');
		redirect(site_url());
		// json_encode($data['cluster_name']);
	}

}